## The following line supports click-mkmindriver and should not be changed.
## Click linuxmodule driver Makefile ##
SHELL = @SHELL@

subdir := linuxmodule
conf_auxdir = @conf_auxdir@

prefix = @prefix@
exec_prefix = @exec_prefix@
bindir = @bindir@
sbindir = @sbindir@
libdir = @libdir@
linuxdir = @linuxdir@

LIB_CXX_OBJS = string.o straccum.o nameinfo.o \
	bitvector.o vectorv.o templatei.o bighashmap_arena.o \
	ipaddress.o ipflowid.o etheraddress.o \
	packet.o \
	error.o timestamp.o glue.o task.o timer.o atomic.o gaprate.o \
	element.o \
	confparse.o variableenv.o lexer.o elemfilter.o \
	routerthread.o router.o master.o handlercall.o notifier.o \
	integers.o iptable.o \
	driver.o ino.o \
	$(EXTRA_DRIVER_OBJS)

EXTRA_DRIVER_OBJS = @EXTRA_DRIVER_OBJS@

LIB_C_OBJS = crc32.o in_cksum.o

LINUXMODULE_CXX_OBJS = config.o sched.o module.o clickfs.o skbmgr.o

LINUXMODULE_C_OBJS = kernelversion.o read-pmc.o

STD_ELEMENT_OBJS = addressinfo.o alignmentinfo.o \
	errorelement.o portinfo.o scheduleinfo.o

ifndef MINDRIVER
DRIVER = click
ELEMENTSCONF = elements
KSYMS = ksyms
else
DRIVER = $(MINDRIVER)click
ELEMENTSCONF = elements_$(MINDRIVER)
KSYMS = ksyms_$(MINDRIVER)
endif

INSTALLOBJS = $(DRIVER).o proclikefs.o


ifeq (@LINUXMODULE_2_6@,1)

##########################################################################
## LINUX 2.6								##

KERNELPATH = @linuxdir@

ifeq ($(KBUILD_EXTMOD),)
top_srcdir := @top_srcdir@
srcdir := @srcdir@
top_builddir := ..
else
ifeq (@srcdir@,.)
top_srcdir := $(src)/..
srcdir := $(src)
else
ifeq ($(patsubst /%,/,@srcdir@),/)
top_srcdir := @top_srcdir@
srcdir := @srcdir@
else
top_srcdir := $(obj)/@top_srcdir@
srcdir := $(obj)/@srcdir@
endif
endif
top_builddir := $(obj)/..
endif

CLICKINSTALL = @INSTALL@
CLICKINSTALL_DATA = @INSTALL@ -m 644
CLICKmkinstalldirs = $(conf_auxdir)/mkinstalldirs

CLICK_ELEM2MAKE = $(top_builddir)/click-buildtool elem2make --linux26

CLICKCC = @KERNEL_CC@
CLICKCXX = @KERNEL_CXX@

LINUXCFLAGS = $(shell echo "$(CPPFLAGS) $(CFLAGS)" | sed \
	-e s,-fno-unit-at-a-time,, -e s,-Wstrict-prototypes,, \
	-e s,-Wdeclaration-after-statement,, \
	-e s,-Wno-pointer-sign,, -e s,-fno-common,,)
CLICKCPPFLAGS = @CPPFLAGS@ -DCLICK_LINUXMODULE
CLICKCFLAGS = @CFLAGS_NDEBUG@
CLICKCXXFLAGS = @CXXFLAGS_NDEBUG@
CLICKDEPCFLAGS = -Wp,-MD,$(depfile)

CLICKDEFS = @DEFS@ 
CLICKINCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include \
	-I$(srcdir) -I$(top_srcdir) -I$(linuxdir)/include

CXXCOMPILE = $(CLICKCXX) $(LINUXCFLAGS) $(CLICKCPPFLAGS) $(CLICKCFLAGS) \
	$(CLICKCXXFLAGS) $(CLICKDEFS) $(CLICKINCLUDES) $(CLICKDEPCFLAGS)
COMPILE = $(CLICKCC) $(LINUXCFLAGS) $(CLICKCPPFLAGS) $(CLICKCFLAGS) \
	$(CLICKDEFS) $(CLICKINCLUDES) $(CLICKDEPCFLAGS)

quiet_cmd_cxxcompile = CXX $(quiet_modtag) $(subst $(obj)/,,$@)
cmd_cxxcompile = $(CXXCOMPILE) -c -o $@ $<

quiet_cmd_ccompile = CC $(quiet_modtag) $(subst $(obj)/,,$@)
cmd_ccompile = $(COMPILE) -c -o $@ $<

EXTRA_CFLAGS = $(CLICKCPPFLAGS) $(CLICKCFLAGS) $(CLICKDEFS) $(CLICKINCLUDES) 

obj-m += $(INSTALLOBJS)

$(DRIVER)-objs = $(LIB_C_OBJS) $(LIB_CXX_OBJS) $(STD_ELEMENT_OBJS) \
	$(ELEMENT_OBJS) $(LINUXMODULE_C_OBJS) $(LINUXMODULE_CXX_OBJS) \
	$(KSYMS).o $(ELEMENTSCONF).o
proclikefs-objs = proclikefsmod.o

KSYM_OBJS = $(LIB_C_OBJS) $(LIB_CXX_OBJS) $(STD_ELEMENT_OBJS) \
	$(ELEMENT_OBJS) $(LINUXMODULE_C_OBJS) $(LINUXMODULE_CXX_OBJS)

all: Makefile $(ELEMENTSCONF).mk $(ELEMENTSCONF).cc always
	$(MAKE) -C $(KERNELPATH) M=$(shell pwd) modules


$(addprefix $(obj)/,$(LIB_CXX_OBJS)): $(obj)/%.o : $(top_srcdir)/lib/%.cc
	$(call if_changed_dep,cxxcompile)

$(addprefix $(obj)/,$(LIB_C_OBJS)): $(obj)/%.o : $(top_srcdir)/lib/%.c
	$(call if_changed_dep,ccompile)

$(addprefix $(obj)/,$(LINUXMODULE_CXX_OBJS)): $(obj)/%.o : $(srcdir)/%.cc
	$(call if_changed_dep,cxxcompile)

$(addprefix $(obj)/,$(LINUXMODULE_C_OBJS)): $(obj)/%.o : $(srcdir)/%.c
	$(call if_changed_dep,ccompile)

$(addprefix $(obj)/,$(STD_ELEMENT_OBJS)): $(obj)/%.o : $(top_srcdir)/elements/standard/%.cc
	$(call if_changed_dep,cxxcompile)

$(obj)/$(ELEMENTSCONF).o : $(obj)/$(ELEMENTSCONF).cc
	$(call if_changed_dep,cxxcompile)

$(obj)/proclikefsmod.o : $(srcdir)/proclikefs.c
	$(call if_changed_rule,cc_o_c)

$(obj)/$(KSYMS).c: $(addprefix $(obj)/,$(KSYM_OBJS)) $(top_builddir)/click-buildtool $(addprefix $(obj)/,$(ELEMENTSCONF).mk)
	$(top_builddir)/click-buildtool ksyms -C $(obj) -x kernelversion.o $(KSYM_OBJS) > $(obj)/$(KSYMS).c
$(obj)/$(KSYMS).o: $(obj)/$(KSYMS).c
	$(call if_changed_dep,ccompile)

ifneq ($(MAKECMDGOALS),clean)
-include $(obj)/$(ELEMENTSCONF).mk
endif

# Don't install in Linux directories for now
# $(MAKE) -C $(KERNELPATH) M=$(shell pwd) modules_install
install: Makefile $(ELEMENTSCONF).mk $(ELEMENTSCONF).cc all
	$(CLICKmkinstalldirs) $(DESTDIR)$(libdir)
	for i in $(patsubst %.o,%.ko,$(INSTALLOBJS)); do $(CLICKINSTALL_DATA) $$i $(DESTDIR)$(libdir)/$$i; done
install-man:
	:
uninstall:
	for i in $(patsubst %.o,%.ko,$(INSTALLOBJS)); do /bin/rm -f $(DESTDIR)$(libdir)/$$i; done

else

##########################################################################
## LINUX 2.4								##

top_srcdir := @top_srcdir@
srcdir := @srcdir@
top_builddir := ..

VPATH = .:$(top_srcdir)/lib:$(top_srcdir)/$(subdir):$(top_srcdir)/elements/standard

CC = @KERNEL_CC@
CPP = @CPP@
CXX = @KERNEL_CXX@
CXXCPP = @CXXCPP@
LD = @LD@
STRIP = @STRIP@
INSTALL = @INSTALL@
INSTALL_DATA = @INSTALL@ -m 644
mkinstalldirs = $(conf_auxdir)/mkinstalldirs
CLICK_ELEM2MAKE = $(top_builddir)/click-buildtool elem2make

ifeq ($(V),1)
ccompile = $(COMPILE) $(1)
cxxcompile = $(CXXCOMPILE) $(1)
else
ccompile = @/bin/echo ' ' $(2) $< && $(COMPILE) $(1)
cxxcompile = @/bin/echo ' ' $(2) $< && $(CXXCOMPILE) $(1)
endif

.SUFFIXES:
.SUFFIXES: .c .cc .o .S .s .i .ii

.c.o:
	$(call ccompile,-c $< -o $@,CC)
.c.i:
	$(call ccompile,-E $< > $@,CPP)
.s.o:
	$(call ccompile,-c $< -o $@,ASM)
.S.o:
	$(call ccompile,-c $< -o $@,ASM)
.cc.o:
	$(call cxxcompile,-c $< -o $@,CXX)
.cc.s:
	$(call cxxcompile,-S $< -o $@,CXX -S)
.cc.ii:
	$(call cxxcompile,-E $< > $@,CXXCPP)


OBJS = $(LIB_CXX_OBJS) $(LIB_C_OBJS) $(STD_ELEMENT_OBJS) $(ELEMENT_OBJS) \
	$(LINUXMODULE_CXX_OBJS) $(LINUXMODULE_C_OBJS) $(ELEMENTSCONF).o

CPPFLAGS = @CPPFLAGS@ -DCLICK_LINUXMODULE
CFLAGS = @CFLAGS_NDEBUG@
CXXFLAGS = @CXXFLAGS_NDEBUG@
DEPCFLAGS = @DEPCFLAGS@

DEFS = @DEFS@ 
INCLUDES = -I$(top_builddir)/include -I$(top_srcdir)/include \
	-I$(srcdir) -I$(top_srcdir) -I$(linuxdir)/include
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@

CXXCOMPILE = $(CXX) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CXXFLAGS) $(DEPCFLAGS)
CXXLD = $(CXX)
CXXLINK = $(CXXLD) $(CXXFLAGS) $(LDFLAGS) -o $@
COMPILE = $(CC) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS) $(DEPCFLAGS)
CCLD = $(CC)
LINK = $(CCLD) $(CFLAGS) $(LDFLAGS) -o $@

all: $(INSTALLOBJS)

ifneq ($(MAKECMDGOALS),clean)
-include $(ELEMENTSCONF).mk
endif

$(DRIVER).o: Makefile $(OBJS)
	$(LD) -r -o $(DRIVER).o $(OBJS)
	$(STRIP) -g $(DRIVER).o

proclikefs.o: proclikefs.c Makefile
	$(COMPILE) -D__KERNEL__ -DMODULE -c $< -o $@

DEPFILES := $(wildcard *.d)
ifneq ($(DEPFILES),)
include $(DEPFILES)
endif

install: $(INSTALLOBJS)
	$(mkinstalldirs) $(DESTDIR)$(libdir)
	for i in $(INSTALLOBJS); do $(INSTALL_DATA) $$i $(DESTDIR)$(libdir)/$$i; done
install-man:
	:
uninstall:
	for i in $(INSTALLOBJS); do /bin/rm -f $(DESTDIR)$(libdir)/$$i; done

endif


##########################################################################
## ANY LINUX								##

elemlist:
	@/bin/rm -f elements.conf
	@$(MAKE) elements.conf
elements.conf: $(top_builddir)/config.status $(top_builddir)/click-buildtool
	echo "linuxmodule @element_groups@" | $(top_builddir)/click-buildtool findelem -r linuxmodule -p $(top_srcdir) > elements.conf
$(ELEMENTSCONF).mk: $(ELEMENTSCONF).conf $(top_builddir)/click-buildtool
	$(CLICK_ELEM2MAKE) -x "$(STD_ELEMENT_OBJS)" < $(ELEMENTSCONF).conf > $(ELEMENTSCONF).mk
$(ELEMENTSCONF).cc: $(ELEMENTSCONF).conf $(top_builddir)/click-buildtool
	$(top_builddir)/click-buildtool elem2export < $(ELEMENTSCONF).conf > $(ELEMENTSCONF).cc
	@rm -f $(ELEMENTSCONF).d

Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
	cd $(top_builddir) \
	  && CONFIG_FILES=$(subdir)/$@ CONFIG_HEADERS= $(SHELL) ./config.status

always:
	@:

clean:
	-rm -f *~ *.d *.o *.mod.c *.ko
	-rm -f .version .*.o.flags .*.o.d .*.o.cmd .*.ko.cmd
	-rm -f $(ELEMENTSCONF).mk $(ELEMENTSCONF).cc $(KSYMS).c elements.conf
distclean: clean
	-rm -f Makefile

.PHONY: all clean distclean elemlist install uninstall always
